#ifndef _MODBUS_H
#define _MUDBUS_H

#define BUFFER_SIZE 256
#define FUNCTION_READ_DI 0x02
#define FUNCTION_READ_AI 0x03
#define FUNCTION_READ_DI_COUNT 0x04
#define FUNCTION_WRITE_DO 0x05 // 继电器输出
#define FUNTION_CHANGE_IO_ADDRESS 0x46
class modbus
{
public:
    modbus(unsigned char addr);
    ~modbus();

    int read_DI();
    int read_AI();
    int read_DI_count();
    int write_DO(unsigned char channel_high, unsigned char channel_low);
    int change_IO_address(unsigned char new_address);
    //-----------------------------------------------------------------
    int response(unsigned char *data, int data_len);
    int response_read_DI(unsigned char *data, int data_len);
    int response_read_AI(unsigned char *data, int data_len);

    //-----------------------------------------------------------------
    unsigned char *get_data();
    int getBinary(int n, int k);
    int parse(unsigned char *data, int data_len);

private:
    unsigned char m_address;
    unsigned char m_data[BUFFER_SIZE];
    unsigned short modbus_crc_16(unsigned char *adata, unsigned int asize);

    //-----------------------------------------------------------------
    int command(unsigned char address, unsigned char function, unsigned char *data, int data_len);
   
};

#endif